package com.tbit.uqbike.webmanager.util.weathers;


import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.value.WeatherResponse;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.http.HttpRequestUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jing
 * @date 2020/11/1118:50
 * @Description:
 */
public class WeatherUtil {
    private static final String succ = "200";

    public static WeatherResponse.NowBean getNowWeather(Double lon, Double lat) {
        if (null == lon || null == lat) {
            throw new BaseException(SystemCodeEnum.LAT_AND_LON_IS_NOT_NULL);
        }
        List<NameValuePair> pairs = new ArrayList<>();
        String location = lon.toString() + "," + lat.toString();
        pairs.add(new BasicNameValuePair("key", OperaPswConstant.WEATHER_KEY));
        pairs.add(new BasicNameValuePair("location", location));
        /**获取和风天气api的天气对象*/
        WeatherResponse weather = JSONObject.parseObject(HttpRequestUtils.doGet(OperaPswConstant.WEATHER_URL, pairs, null), WeatherResponse.class);
        if (!StringUtils.equals(succ, weather.getCode())) {
            throw new BaseException(SystemCodeEnum.FAILED_TO_GET_WEATHER);
        }
        return weather.getNow();
    }
}
